﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoyoEden.Web.Data
{
	[Serializable]
	public struct SimplePager
	{
		#region properties
		/// <summary>
		/// 每页记录数
		/// </summary>
		public int RowPerPage
		{
			get;
			set;
		}
		/// <summary>
		/// 记录总数
		/// </summary>
		public int RowTotal
		{
			get;
			set;
		}
		/// <summary>
		/// 当前页数
		/// </summary>
		public int CurrentPage
		{
			get;
			set;
		}
		/// <summary>
		/// 页总数
		/// </summary>
		public int PageTotal
		{
			get;
			set;
		}
		/// <summary>
		/// 分页提示信息
		/// </summary>
		public string Tip
		{
			get;
			set;
		}
		#endregion

		/// <summary>
		/// 
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="inData"></param>
		/// <param name="rowPerPage"></param>
		/// <param name="currentPage"></param>
		/// <param name="outData"></param>
		public void PagedList<T>(List<T> inData, int rowPerPage, int currentPage,out List<T> outData)
		{
			int totalRecord = inData.Count;//所有行数
			int pageNum = (totalRecord % rowPerPage) > 0 ? (totalRecord / rowPerPage + 1) : (totalRecord / rowPerPage);//页数

			this.CurrentPage = currentPage;
			this.RowPerPage = rowPerPage;
			this.RowTotal = totalRecord;
			this.PageTotal = pageNum;

			int beginIdex = (currentPage - 1) * rowPerPage;
			int endIndex = (currentPage == pageNum) ? (totalRecord - 1) : (currentPage * rowPerPage - 1);

			outData = new List<T>();

			for (int i = beginIdex; i <= endIndex; i++)
			{
				outData.Add(inData[i]);
			}

			StringBuilder sb = new StringBuilder();
			sb.AppendFormat("显示第{0}至{1},共{2}条记录", beginIdex + 1, endIndex + 1, totalRecord);
			this.Tip = sb.ToString();

		}//endof PagedListT
	}
}
